package com.example.dangerouscargodriver.ui.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.baiju.netmanager.Http.IHttpRequest;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.ui.fragment.HttpRequestFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.OrderManagerTabAllFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.OrderManagerTabAllFragmentWrapper;
import com.example.dangerouscargodriver.ui.fragment.tab.OrderManagerTabCancelFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.OrderManagerTabCancelFragmentWrapper;
import com.example.dangerouscargodriver.ui.fragment.tab.OrderManagerTabCloseFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.OrderManagerTabCloseFragmentWrapper;
import com.example.dangerouscargodriver.ui.fragment.tab.OrderManagerTabDiscussFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.OrderManagerTabDiscussFragmentWrapper;
import com.example.dangerouscargodriver.ui.fragment.tab.OrderManagerTabDoneFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.OrderManagerTabDoneFragmentWrapper;
import com.example.dangerouscargodriver.ui.fragment.tab.OrderManagerTabFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.OrderManagerTabTransportFragment;
import com.example.dangerouscargodriver.ui.fragment.tab.OrderManagerTabTransportFragmentWrapper;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyOrderManagerActivity extends AppCompatActivity {

    private static final String TAG = "OrderManagerActivity";
    private ActionBar mActionBar = null;
    private String[] mTabs = {"全部", "议价中", "运输中", "已完成", "取消中", "已失效"};

    private int[] mTabsEnum = {
            OrderManagerTabAllFragment.TAB_ENUM_ALL,
            OrderManagerTabAllFragment.TAB_ENUM_DISCUSSING,
            OrderManagerTabAllFragment.TAB_ENUM_TRANSPORTING,
            OrderManagerTabAllFragment.TAB_ENUM_DONE,
            OrderManagerTabAllFragment.TAB_ENUM_CANCEL,
            OrderManagerTabAllFragment.TAB_ENUM_LOSE_EFFICACY
    };

    private List<OrderManagerTabFragment> mTabFragmentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_manager);

        TabLayout tabLayout = findViewById(R.id.tab_layout);
        ViewPager viewPager = findViewById(R.id.view_pager);

        //添加tab
        tabLayout.addTab(tabLayout.newTab().setText(mTabs[0]));
        mTabFragmentList.add(new OrderManagerTabAllFragmentWrapper());
        tabLayout.addTab(tabLayout.newTab().setText(mTabs[1]));
        mTabFragmentList.add(new OrderManagerTabDiscussFragmentWrapper());
        tabLayout.addTab(tabLayout.newTab().setText(mTabs[2]));
        mTabFragmentList.add(new OrderManagerTabTransportFragmentWrapper());
        tabLayout.addTab(tabLayout.newTab().setText(mTabs[3]));
        mTabFragmentList.add(new OrderManagerTabDoneFragmentWrapper());
        tabLayout.addTab(tabLayout.newTab().setText(mTabs[4]));
        mTabFragmentList.add(new OrderManagerTabCancelFragmentWrapper());
        tabLayout.addTab(tabLayout.newTab().setText(mTabs[5]));
        mTabFragmentList.add(new OrderManagerTabCloseFragmentWrapper());

        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager(),
                FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {

                return mTabFragmentList.get(position);
            }

            @Override
            public int getCount() {

                return mTabFragmentList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {

                return mTabs[position];
            }
        });

        //设置TabLayout和ViewPager联动
        tabLayout.setupWithViewPager(viewPager,true);
        Intent intent = getIntent();
        tabLayout.getTabAt(intent.getIntExtra("TabIndex",0)).select();

        mActionBar = findViewById(R.id.actionBarOrderManager);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}